package com.vipymh.springbootmybatisdemo.controller;

import com.vipymh.springbootmybatisdemo.pojo.Result;
import com.vipymh.springbootmybatisdemo.utils.AliOssUtil;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
public class FileUploadController {

    @PostMapping("/upload")
    public Result<String> upload(MultipartFile file) throws Exception {

        // 文件的内容存储到本地磁盘
        String originalFilename = file.getOriginalFilename();
        // 文件名唯一
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        // file.transferTo(new File("C:\\Users\\Administrator\\Desktop\\file\\" + fileName));
        String url = AliOssUtil.uploadFile(fileName,file.getInputStream());
        return Result.success(url);
    }
}
